package com.juno.cafeNoa.admin.adapter;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.juno.cafeNoa.admin.R;
import com.juno.cafeNoa.admin.data.Order;
import com.juno.cafeNoa.admin.listener.OrderListCompelete;
import com.juno.cafeNoa.admin.net.AsyncTaskCompleteListener;
import com.juno.cafeNoa.admin.net.HttpPostTask;
import com.juno.cafeNoa.admin.net.NetConstant;
import com.juno.cafeNoa.admin.util.Util;

public class OrderADP extends ArrayAdapter<Order> {
	private Context mContext;
	private int mResource;
	private ArrayList<Order> mList;
	private LayoutInflater mInflater;
	private int clickPosition;
	
	private OrderListCompelete mListener;
	
	
	public OrderADP(Context context, int layoutResource, ArrayList<Order> list) {
		super(context, layoutResource, list);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = list;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setListener(OrderListCompelete listener){
		this.mListener = listener;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || convertView.getId() !=mResource) {
			convertView = mInflater.inflate(mResource, null);
			holder = new ViewHolder();
			holder.id = (TextView) convertView.findViewById(R.id.orderId);
			holder.content = (TextView) convertView.findViewById(R.id.orderContent);
			holder.comment = (TextView) convertView.findViewById(R.id.orderComment);
			holder.price = (TextView) convertView.findViewById(R.id.orderPrice);
			holder.pickup = (TextView) convertView.findViewById(R.id.orderPickup);
			holder.method = (TextView) convertView.findViewById(R.id.orderMethod);
			holder.complete = (Button) convertView.findViewById(R.id.orderComplete);

			convertView.setTag(holder);
			convertView.setId(R.layout.row_order);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.id.setText(String.valueOf(mList.get(position).getOrderId()));
		holder.content.setText(mList.get(position).getContent());
		holder.comment.setText(mList.get(position).getComment());
		holder.price.setText(String.valueOf(mList.get(position).getPrice())+"원");
		holder.pickup.setText(mList.get(position).getPickup());
		
		String temp = null;
		if(mList.get(position).getMethod().equalsIgnoreCase("V")){
			temp = "방문";
			holder.content.setTextColor(Color.RED);
		}else if(mList.get(position).getMethod().equalsIgnoreCase("P")){
			temp = "포인트";
			holder.content.setTextColor(Color.BLUE);
		}else if(mList.get(position).getMethod().equalsIgnoreCase("C")){
			temp = "카드";
			holder.content.setTextColor(Color.GREEN);
		}
		
		holder.method.setText(temp);
		holder.complete.setText("완료");
		holder.complete.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clickPosition = position;
			
				AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
				ab.setTitle("확인");
				ab.setIcon(R.drawable.ic_launcher);
				ab.setMessage("주문을 완료하시겠습니까 ?");
				ab.setPositiveButton("완료", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						orderComplete.excuteHttpTask();
					}
				});

				ab.setNegativeButton("종료", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});

				AlertDialog alert = ab.create();
				alert.show();
			}
			
		});
		return convertView;
	}

	class ViewHolder {
		TextView id;
		TextView content;
		TextView comment;
		TextView price;
		TextView pickup;
		TextView method;
		Button complete;
		

		public TextView getId() {
			return id;
		}

		public void setId(TextView id) {
			this.id = id;
		}

		public TextView getContent() {
			return content;
		}

		public void setContent(TextView content) {
			this.content = content;
		}

		public TextView getComment() {
			return comment;
		}

		public void setComment(TextView comment) {
			this.comment = comment;
		}

		public TextView getPrice() {
			return price;
		}

		public void setPrice(TextView price) {
			this.price = price;
		}

		public TextView getPickup() {
			return pickup;
		}

		public void setPickup(TextView pickup) {
			this.pickup = pickup;
		}

		public TextView getMethod() {
			return method;
		}

		public void setMethod(TextView method) {
			this.method = method;
		}

		public Button getComplete() {
			return complete;
		}

		public void setComplete(Button complete) {
			this.complete = complete;
		}
		
	}
	
	AsyncTaskCompleteListener orderComplete = new AsyncTaskCompleteListener() {

		@Override
		public void excuteHttpTask() {
			String url = Util.getUrl("CompletePickUp");
			
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			Log.v("???????",String.valueOf(mList.get(clickPosition).getOrderId()));
			value.add(new BasicNameValuePair("ORDER_NUMBER", String.valueOf(mList.get(clickPosition).getOrderId())));
			
			HttpPostTask http = new HttpPostTask(mContext, value, url, orderComplete);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			Log.v("result","reuslt :" + result);
			if(taskOk){
				int code = -1;
				
				try {
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");

					if(code == NetConstant.NETWORK_SUCCESS){
						Toast.makeText(mContext, "정상적으로 처리되었습니다.", Toast.LENGTH_LONG).show();
						mListener.OrderComplete(true, clickPosition);
					} else {
						Toast.makeText(mContext, "서버오류 !.", Toast.LENGTH_LONG).show();
						mListener.OrderComplete(false, -1);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	};
}
